package com.yanqu.road.server.cmd.newmonopolymkt;

import com.yanqu.road.entity.monopolymarket.office.MonopolyMarketOfficeTip;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

/**
 * // 为什么需要校验
 * 因为有可能 位置变动跨服没同步到区服
 *
 */
@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_GET_USER, desc = "榷场获取玩家数据结果  跨服返回区服 协议")
public class NewMonopolyMarketGetUserFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketCheckUserDataToGameServerMsg msg = NewMonopolyMarketProto.NewMonopolyMarketCheckUserDataToGameServerMsg.parseFrom(packet.getBytes());
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
        if(marketModule.userData == null){
            return;
        }
        if(msg.hasRobedRecord()){
            // 先结算到原来被抢的时间(包括被抢、及周日22点的每周结算)
            marketModule.userData.setOfficeCurPos(msg.getRobedRecord().getPosIndex());
            marketModule.userData.setOfficeLevel(msg.getRobedRecord().getLevel());
            marketModule.userData.setOfficeNo(msg.getRobedRecord().getNo());
            NewMonopolyMarketMgr.refreshOfficeTeaReward(marketModule.userData, msg.getRobedRecord().getTime());
            NewMonopolyMarketMgr.refreshGuanYin(marketModule.userData, msg.getRobedRecord().getTime(),true);
            if(marketModule.getInSystemId() >= 0){
                //周日22：00后的不弹框
                if(!StringUtils.isNullOrEmpty(msg.getRobedRecord().getName())) {
                    NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.Builder builder = NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.newBuilder();
                    builder.setType(1);
                    NewMonopolyMarketProto.NewMonopolyMarketOfficeTipEntity.Builder tipBuilder = NewMonopolyMarketProto.NewMonopolyMarketOfficeTipEntity.newBuilder();
                    tipBuilder.setRobName(msg.getRobedRecord().getName());
                    tipBuilder.setLevel(msg.getRobedRecord().getLevel());
                    tipBuilder.setNo(msg.getRobedRecord().getNo());
                    builder.setOfficeTip(tipBuilder);
                    player.sendPacket(YanQuMessageUtils.buildMessage(ClientProtocol.U_NEW_MONOPOLY_MARKET_BROADCAST, builder));
                }
            }else {
                MonopolyMarketOfficeTip officeTip = new MonopolyMarketOfficeTip();
                officeTip.robName = msg.getRobedRecord().getName();
                officeTip.level = msg.getRobedRecord().getLevel();
                officeTip.no = msg.getRobedRecord().getNo();
                marketModule.userData.setOfficeTip(officeTip);
                marketModule.userData.setHasTip(true);
            }
        }else {
            // 检查配置 分组是否发生变化
            // marketModule.checkUserData();
        }

        // 周重置玩家
        if(!NewMonopolyMarketMgr.resetUser(marketModule)){
            // 同步跨服下发下来的数据
            marketModule.userData.setOfficeCurPos(msg.getOfficePosIndex());
            marketModule.userData.setOfficeLevel(msg.getOfficePosLevel());
            marketModule.userData.setOfficeNo(msg.getOfficePosNo());
            marketModule.userData.setOfficeSeatTime(msg.getOfficeSeatTime());
            // 同步派遣信息
            marketModule.getCurDispatchInfoListMap().clear();
            for (NewMonopolyMarketProto.NewMonopolyMarketSynDispatchInfoEntity item : msg.getDispatchInfoListList()){
                marketModule.addDispatchInfo(item.getResGroupId(), item.getType(),item.getPosIndex(),item.getDispatchObjectIdList(),item.getStartTime());
            }
        }

        if(msg.hasHasTip()) {
            marketModule.userData.setHasTip(msg.getHasTip());
        }
        if(msg.hasGetUserReward()) {
            marketModule.userData.setGetUserReward(msg.getGetUserReward());
        }
        if(msg.hasGetServerReward()) {
            //该标识以区服为准
            //marketModule.userData.setGetServerReward(msg.getGetServerReward());
        }

        NewMonopolyMarketMgr.refreshUser(player,true,true);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
